package com.cwnu.sw.cys.core.user.comsumer;

import com.cwnu.sw.cys.core.common.constant.MQConstant;
import com.cwnu.sw.cys.core.user.domain.dto.LoginMessageDTO;
import com.cwnu.sw.cys.core.websocket.service.WebSocketService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 登录消息消费者类
 * 监听登录消息主题，并处理接收到的登录消息
 * @author jisam
 */
@RocketMQMessageListener(consumerGroup = MQConstant.LOGIN_MSG_GROUP, topic = MQConstant.LOGIN_MSG_TOPIC, messageModel = MessageModel.BROADCASTING)
@Component
@Slf4j
public class MsgLoginConsumer implements RocketMQListener<LoginMessageDTO> {
    @Autowired
    private WebSocketService webSocketService;

    /**
     * 处理接收到的登录消息
     * 尝试将登录信息应用到系统中
     *
     * @param loginMessageDTO 登录消息的详细信息，包含登录所需的验证码和用户ID
     */
    @Override
    public void onMessage(LoginMessageDTO loginMessageDTO) {
        //尝试登录登录
        webSocketService.scanLoginSuccess(loginMessageDTO.getCode(), loginMessageDTO.getUid());
        log.info("登录成功");
    }
}
